package routers

import (
	"backend/middleware/jwt"
	"backend/pkg/setting"
	"backend/routers/api/test"
	"backend/routers/api/v1"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {
	gin.SetMode(setting.RunMode)
	r := gin.New()

	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	// session
	store, _ := redis.NewStore(20, "tcp", setting.RedisConfig, "", []byte(setting.RandCode))
	r.Use(sessions.Sessions("xin", store))
	// 路由
	r.GET("/auth", v1.GetAuth)
	//websocket
	go test.Manager.Start()
	r.GET("/ws", func(c *gin.Context) {
		test.WsPage(c.Writer, c.Request)
	})
	r.GET("/user/regist", v1.Regist)
	r.GET("/user/login", v1.Login)
	apiv1 := r.Group("/api/v1")
	apiv1.Use(jwt.JWT())
	{
		apiv1.GET("/tags", v1.GetTags)
		apiv1.POST("/tags", v1.AddTag)
		apiv1.PUT("/tags/:id", v1.EditTag)
		apiv1.DELETE("/tags/:id", v1.DeleteTag)

		apiv1.GET("/articles", v1.GetArticles)
		apiv1.GET("/articles/:id", v1.GetArticle)
		apiv1.POST("/articles", v1.AddArticle)
		apiv1.PUT("/articles/:id", v1.EditArticle)
		apiv1.DELETE("/articles/:id", v1.DeleteArticle)
	}
	return r
}
